<template>
  <div class="tab-pane fade" :id="id" role="tabpanel">
    <div class="card col-12">
      <div class="card-body">
        <form id="frmWeixinServiceConfig" class="form-horizontal">
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">租户ID</label>
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="enterpriseId"
                readonly
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">开发者ID</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="appId" />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end"
              >开发者密码</label
            >
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="appSecret"
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">令牌</label>
            <div class="col-4 me-auto">
              <input type="input" class="form-control" v-model.trim="token" />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end"
              >消息加解密密钥</label
            >
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="encodingKey"
              />
              <span class="text-muted small"></span>
            </div>
          </div>
          <div class="row">
            微信公众平台网址：
            <a href="https://mp.weixin.qq.com/" target="_blank"
              >https://mp.weixin.qq.com/</a
            >
          </div>
          <div class="row">
            <button
              id="btnChange"
              type="button"
              class="btn btn-primary  col-4 offset-4"
              :disabled="saving"
              @click.stop="saveWeixinServiceConfig()"
            >
              保存
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import {
  searchWeixinServiceConfig,
  saveWeixinServiceConfig,
} from "@/api/admin.js";

export default {
  props: {
    id: {
      type: String,
    },
  },
  data() {
    return {
      enterpriseId: 0,
      appId: "",
      appSecret: "",
      token: "",
      encodingKey: "",

      saving: false,
    };
  },
  mounted: function () {
    this.searchWeixinServiceConfig();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    searchWeixinServiceConfig: function () {
      searchWeixinServiceConfig((v) => {
        this.enterpriseId = v.enterpriseId;
        this.appId = v.appId;
        this.appSecret = v.appSecret;
        this.token = v.token;
        this.encodingKey = v.encodingKey;
      });
    },
    saveWeixinServiceConfig: function () {
      const params = {
        appId: this.appId,
        appSecret: this.appSecret,
        token: this.token,
        encodingKey: this.encodingKey,
      };

      this.saving = true;

      saveWeixinServiceConfig(
        params,
        (v) => {
          if (v.status === "OK") this.showErrMsg("保存成功");
          else this.showErrMsg(v.errmsg, "danger");
        },
        () => {
          this.saving = false;
        }
      );
    },
  },
};
</script>